<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class login extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $this->load->view('login.html');
    }

//登陆
    public function login()
    {
        $name = $this->input->post('name');
        $password = md5($this->input->post('password'));

//      echo   md5(123456);
//      die;
        $sql = "SELECT * FROM sys_admin WHERE name = ? and password=?";
        $result = $this->db->query($sql, array($name, $password))->result_array();
//        var_dump($result);
//        die;
        if ($result) {
            $_SESSION['adminName'] = $result[0]['name'];
            $_SESSION['adminId'] = $result[0]['id'];
            $this->alert_message(1, '登录成功');
//            die;
            $ret = $this->getPageByLevel($result[0]['level'], $result[0]['id']);

            $pageList = $this->getListPageById($result[0]['id']);
            $_SESSION['page'] = $ret;
            $_SESSION['pagelist'] = $pageList;

            //$result= $this->playerlist();

            $data['data'] = array($result, $_SESSION);
            $this->load->view('head.html', $data);
            $this->load->view('index.html');
            $this->load->view('footer.html');
        } else {
            $url = site_url() . "/login/index";
            $this->alert_message(0, '登录失败', $url);
        }
    }

    function logout()
    {
        $_SESSION = array();
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time() - 1);
        }
        if (isset($_COOKIE['adminId'])) {
            setcookie('adminId', '', time() - 1);
        }
        if (isset($_COOKIE['adminName'])) {
            setcookie('adminName', '', time() - 1);
        }
        session_destroy();
        header("location:index");
    }

    function getPageByLevel($level, $uid = '')
    {
        //     SELECT id from sys_page where ulevel<=2 or uid=2;

//        "invalid" => 0,
        $array = array(
            'uid' => $uid
        );
        $array2 = array(
            'ulevel >='=>$level,
            'superior_id' => 0
        );
        $result = $this->db
            ->select('id')
            ->where($array2)
            ->or_where($array)
            ->get('sys_page')->result_array();

        $ret = array();
        foreach ($result as $value) {
            foreach ($value as $v) {
                array_push($ret, $v);
            }
        }
        return $ret;
    }

    function getListPageById($uid)
    {
        $array = array(
            'id' => $uid
        );
        $result = $this->db
            ->select('page')
            ->where($array)
            ->get('sys_admin')->result_array();
        $pageList =  explode(',',$result[0]['page']) ;

        return $pageList;
    }
//玩家信息
    function playerlist()
    {

//        $DB2 = $this->load->database('account', true);
//
//
//        $array = array();
////        $array = array("invalid" => 0);
//        $result = $DB2
//            ->select('UserID,Accounts,NickName,RegisterDate,Gender,RoomCard')
//            ->where($array)
//            ->get('AccountsInfo')->result_array();
//
//        return $result;
        $result=array();
        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('index.html');
        $this->load->view('footer.html');
    }

}